<?php
namespace x\my;
use ReflectionFunction;

class Format
{
    /**
     * 格式化
     * @param  [type] $data [description]
     * @param  [type] $rule [description]
     * @return [type]       [description]
     */
    public static function make($data, $rule,$ext=[]){
        $temp = [];
        foreach($data as $k => $v){
            foreach($v as $field=>$value){
                if(isset($rule[$field])){
                    $v[$field] = self::makeRule($rule[$field], $value,$v,$ext);
                }
            }
            $temp[$k] = $v;
        }
        return $temp;
    }

    /**
     * 执行规则
     * @param  [type] $rule  [description]
     * @param  [type] $value [description]
     * @return [type]        [description]
     */
    public static function makeRule($rule, $v,$val=[],$ext=[]){
        $temp = '';
        switch ($rule) {
            case 'com_time_format':
                $temp = com_time_format($v);
                break;
            case 'com_date_format':
                    $temp = com_time_format($v,'Y-m-d');
                    break;
            default:
                $reflect = new ReflectionFunction($rule);
                $temp = $reflect->invokeArgs(['v'=>$v, 'val'=>$val,'ext'=>$ext]);
                break;
        }
        return $temp;
    }
}
